package com.augurit.gzcgwsp.mail;

import java.util.Date;
import java.util.Properties;

import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

import org.springframework.stereotype.Component;

import com.augurit.gzcgwsp.excelimportexport.util.FileUtil;
import com.sun.mail.smtp.SMTPTransport;

/**
 * 发送邮件类
 * @author zj
 *
 */
@Component("sendMail")
public class SendMail {
	private static FileUtil fileUtil = new FileUtil();
    // 以下为设置默认邮件内容   
    private String smtp = fileUtil.getValue("mail.properties", "stmp");// 发送服务器   
    private String sendEmail = fileUtil.getValue("mail.properties","mailuser");// 发送邮件邮箱   
    private String sendEmailPw = fileUtil.getValue("mail.properties","mailpassword");// 密码
    
    private String contentType = "text/html;charset=gbk"; 
    public void setSmtp(String smtp) {   
        this.smtp = smtp;   
    }   
  
    public void setSendEmail(String sendEmail) {   
        this.sendEmail = sendEmail;   
    }   
  
    public void setSendEmailPw(String sendEmailPw) {   
        this.sendEmailPw = sendEmailPw;   
    }   
  
    public void setContentType(String contentType) {   
        this.contentType = contentType;   
    }   
  
    /**
     * 发送邮件
     * @param mailTitle
     * @param toEmail
     * @param mailContent
     * @return
     */
    public boolean sendMail(String mailTitle,String toEmail,String mailContent) {   
        boolean isSend = false;   
        try {   
            Session sendMailSession = null;   
            SMTPTransport transport = null;   
            Properties props = new Properties();  
            // 与服务器建立Session的参数设置   
            props.put("mail.smtp.host", smtp); // SMTP服务器。   
            props.put("mail.smtp.auth", "true"); // 将这个参数设为true，让服务器进行认证。   
            SMTPAuthenticator auth = new SMTPAuthenticator(sendEmail,   
                    sendEmailPw); // ，用户名，密码。   
            sendMailSession = Session.getInstance(props, auth); // 建立连接。   
            // SMTPTransport用来发送邮件。   
            transport = (SMTPTransport) sendMailSession.getTransport("smtp");   
            transport.connect();   
            // 创建邮件。   
            Message newMessage = new MimeMessage(sendMailSession);   
            newMessage.setFrom(new InternetAddress(sendEmail));   
            newMessage.setRecipient(Message.RecipientType.TO,   
                    new InternetAddress(toEmail));// 设置发送目标邮件  
            //解决乱码问题
            newMessage.setSubject(MimeUtility.encodeText(mailTitle, "UTF-8", "B"));
            newMessage.setSentDate(new Date());   
            // 使用MimeMultipart和MimeBodyPart才能发HTML格式邮件。   
            BodyPart bodyPart = new MimeBodyPart();   
            bodyPart.setContent(mailContent, contentType); // 发一个HTML格式的   
            Multipart mp = new MimeMultipart();   
            mp.addBodyPart(bodyPart);   
            newMessage.setContent(mp);   
            Transport.send(newMessage);   
            isSend = true;   
        } catch (Exception e) {   
            e.printStackTrace();
        }   
        return isSend;   
    }   
    
    
    
    public static void main(String[] args) {   
        SendMail s=new SendMail();
        s.sendMail("ddd斯蒂芬斯蒂芬的","41278378@qq.com","tes斯蒂芬斯蒂芬t");
        System.out.println("发送完成");   
    }   
	
}
